﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using VietImage.Models;
using VietImage.Supports;
using VietImage.Roles;
using System.Threading;
using System.Windows.Threading;

namespace VietImageSoftware
{
	/// <summary>
	/// Interaction logic for ProgressBar.xaml
	/// </summary>
	public partial class ProgressBarForm : Window
    {
        private int prior;

        public int Prior
        {
            get { return prior; }
            set { prior = value; }
        }

        int currentPage;

        public int CurrentPage
        {
            get { return currentPage; }
            set { currentPage = value; }
        }
        int countPages;

        public int CountPages
        {
            get { return countPages; }
            set { countPages = value; }
        }
        int countPerPage = 8;

        private int countImages;

        public int CountImages
        {
            get { return countImages; }
            set { countImages = value; }
        }

        private RegisteredUserRole userRole;

        public RegisteredUserRole UserRole
        {
            get { return userRole; }
            set { userRole = value; }
        }

        private int id_Command;

        public int Id_Command
        {
            get { return id_Command; }
            set { id_Command = value; }
        }

        private Category currentCategory;

        public Category CurrentCategory
        {
            get { return currentCategory; }
            set { currentCategory = value; }
        }

        private VietImage.Models.Image currentImage;

        public VietImage.Models.Image CurrentImage
        {
            get { return currentImage; }
            set { currentImage = value; }
        }

        private List<DetailCategory> detailCategories;

        public List<DetailCategory> DetailCategories
        {
            get { return detailCategories; }
            set { detailCategories = value; }
        }

        private List<Category> parentCategories;

        public List<Category> ParentCategories
        {
            get { return parentCategories; }
            set { parentCategories = value; }
        }

        private List<DetailImage> detailImages;

        public List<DetailImage> DetailImages
        {
            get { return detailImages; }
            set { detailImages = value; }
        }


        System.Collections.ObjectModel.ObservableCollection<VietImage.Models.Image> omUploadedImages = new System.Collections.ObjectModel.ObservableCollection<VietImage.Models.Image>();
        System.Collections.ObjectModel.ObservableCollection<VietImage.Models.Image> omHistoryImages = new System.Collections.ObjectModel.ObservableCollection<VietImage.Models.Image>();
        public System.Collections.ObjectModel.ObservableCollection<VietImage.Models.Image> OmUploadedImages
        {
            get { return omUploadedImages; }
        }
        public System.Collections.ObjectModel.ObservableCollection<VietImage.Models.Image> OmHistoryImages
        {
            get { return omHistoryImages; }
        }

        private Week currentWeek;

        public Week CurrentWeek
        {
            get { return currentWeek; }
            set { currentWeek = value; }
        }

        private DateTime currentDate;

        public DateTime CurrentDate
        {
            get { return currentDate; }
            set { currentDate = value; }
        }

        private int currentMonth;

        public int CurrentMonth
        {
            get { return currentMonth; }
            set { currentMonth = value; }
        }

        private int currentYear;

        public int CurrentYear
        {
            get { return currentYear; }
            set { currentYear = value; }
        }

		public ProgressBarForm()
		{
			this.InitializeComponent();
			
			// Insert code required on object creation below this point.
		}

        private void window_Loaded(object sender, RoutedEventArgs e)
        {
            switch (id_Command)
            {
                    //Delete category
                case 1:
                    {
                        new Thread(
                            delegate()
                            {
                                DeleteCategory();
                            }).Start();
                        break;
                    }
                    //Set Primary Image
                case 2:
                    {
                        new Thread(
                            delegate()
                            {
                                SetPrimaryImage();
                            }).Start();
                        break;
                    }
                //Manage Image
                case 3:
                    {
                        new Thread(
                            delegate()
                            {
                                ManageImage();
                            }).Start();
                        break;
                    }
                //Choose Parent Categories
                case 4:
                    {
                        new Thread(
                            delegate()
                            {
                                ChooseParentCategories();
                            }).Start();
                        break;
                    }
                //Create Category
                case 5:
                    {
                        new Thread(
                            delegate()
                            {
                                CreateCategory();
                            }).Start();
                        break;
                    }
                //Edit Category
                case 6:
                    {
                        new Thread(
                            delegate()
                            {
                                EditCategory();
                            }).Start();
                        break;
                    }
                //Not publish images
                case 7:
                    {
                        new Thread(
                            delegate()
                            {
                                LoadImagesList(userRole.UserInfo.ID_Permission, currentCategory.ID);
                            }).Start();
                        break;
                    }
                //History images
                case 8:
                    {
                        new Thread(
                            delegate()
                            {
                                LoadImagesList(userRole.UserInfo.ID_Permission);
                            }).Start();
                        break;
                    }
            }
        }

        private void DeleteCategory()
        {
            Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
            {
                lblPBTitle.Content = "Deleting";
                pbPBWaitingBar.Minimum = 0.0;
                pbPBWaitingBar.Maximum = 3.0;
                pbPBWaitingBar.Value = 0.0;

            });
            if (userRole.UserInfo.ID_Permission == 1)
            {
                OwnerRole role = (OwnerRole)userRole;
                Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
                {
                    pbPBWaitingBar.Value++;
                });
                List<ErrorItem> el = new List<ErrorItem>();
                Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
                {
                    el = role.DeleteCategory(currentCategory, false);
                    pbPBWaitingBar.Value++;
                    if (el.Count == 0)
                        el.Add(new ErrorItem("ErrorMessage", ErrorMessage.GetErrorMessage(1, userRole.ID_Language).Description));
                    MessageForm form = new MessageForm();
                    form.ErrorList = el;
                    form.ShowDialog();
                });
                Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
                {
                    lblPBTitle.Content = "Deleted";
                    pbPBWaitingBar.Value++;
                });
            }
            Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
            {
                Thread.Sleep(100);
                Close();
            });
        }

        private void SetPrimaryImage()
        {
            Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
            {
                gridProgressBar.Visibility = Visibility.Visible;
                pbPBWaitingBar.Maximum = 3.0;
                pbPBWaitingBar.Minimum = 0.0;
                pbPBWaitingBar.Value = 0.0;
            });
            if (userRole.UserInfo.ID_Permission == 1)
            {
                OwnerRole role = (OwnerRole)userRole;
                Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
                {
                    pbPBWaitingBar.Value++;
                });
                List<ErrorItem> el = new List<ErrorItem>();
                Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
                {
                    el = role.EditCategoryInformation(currentCategory);
                    pbPBWaitingBar.Value++;
                });
                Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
                {
                    if (el.Count == 0)
                        el.Add(new ErrorItem("ErrorMessage", ErrorMessage.GetErrorMessage(1, userRole.ID_Language).Description));
                    MessageForm form = new MessageForm();
                    form.ErrorList = el;
                    form.ShowDialog();
                    if (el.Count != 0)
                        Close();
                    lblPBTitle.Content = "Finish";
                    pbPBWaitingBar.Value++;
                });
            }
            Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
            {
                Thread.Sleep(100);
                Close();
            });
        }

        private void ManageImage()
        {
            Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
            {
                gridProgressBar.Visibility = Visibility.Visible;
                pbPBWaitingBar.Maximum = (currentImage.GetDetails().Count + 1) * 1.0;
                pbPBWaitingBar.Minimum = 0.0;
                pbPBWaitingBar.Value = 0.0;
                lblPBTitle.Content = "Changing Image";
            });
            List<ErrorItem> el = null;
            Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
            {
                pbPBWaitingBar.Value++;
            });

            foreach (DetailImage di in DetailImages)
            {
                if (userRole.UserInfo.ID_Permission == 1)
                {
                    OwnerRole role = (OwnerRole)userRole;
                    Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
                    {
                        el = role.EditDetailImage(di);
                        pbPBWaitingBar.Value++;
                        if (el.Count > 0)
                        {
                            MessageForm form = new MessageForm();
                            form.ErrorList = el;
                            form.ShowDialog();
                            Close();
                        }
                    });
                }
                else if (userRole.UserInfo.ID_Permission == 4)
                {
                    PartnerRole role = (PartnerRole)userRole;
                    Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
                    {
                        el = role.EditDetailImage(di);
                        pbPBWaitingBar.Value++;
                        if (el.Count > 0)
                        {
                            MessageForm form = new MessageForm();
                            form.ErrorList = el;
                            form.ShowDialog();
                            Close();
                        }
                    });
                }
            }

            if (userRole.UserInfo.ID_Permission == 1)
            {
                OwnerRole role = (OwnerRole)userRole;
                Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
                {
                    el = role.EditImage(currentImage, 0);
                    pbPBWaitingBar.Value++;
                    if (el.Count > 0)
                    {
                        MessageForm form = new MessageForm();
                        form.ErrorList = el;
                        form.ShowDialog();
                        Close();
                    }
                });
            }
            else if (userRole.UserInfo.ID_Permission == 4)
            {
                PartnerRole role = (PartnerRole)userRole;
                Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
                {
                    el = role.EditImage(currentImage, 0);
                    pbPBWaitingBar.Value++;
                    if (el.Count > 0)
                    {
                        MessageForm form = new MessageForm();
                        form.ErrorList = el;
                        form.ShowDialog();
                        Close();
                    }
                });
            }
            Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
            {
                if (el.Count == 0)
                {
                    el.Add(new ErrorItem("ErrorMessage", ErrorMessage.GetErrorMessage(1, userRole.ID_Language).Description));
                    MessageForm form = new MessageForm();
                    form.ErrorList = el;
                    form.ShowDialog();
                    lblPBTitle.Content = "Finish";
                }
            });
            Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
            {
                Thread.Sleep(100);
                Close();
            });
        }

        private void ChooseParentCategories()
        {

            if (userRole.UserInfo.ID_Permission == 1)
            {
                OwnerRole role = (OwnerRole)userRole;
                List<ErrorItem> el = new List<ErrorItem>();
                List<Category> lp = currentCategory.Parents;
                int nAdd = 0;
                Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate { nAdd = parentCategories.Count; });
                int nRemove = 0;
                Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate { nRemove = lp.Count; });

                Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
                {
                    gridProgressBar.Visibility = Visibility.Visible;
                    pbPBWaitingBar.Maximum = (double)(nAdd + nRemove - 1);
                    pbPBWaitingBar.Minimum = 0;
                    pbPBWaitingBar.Value = 0;
                });
                for (int i = 0; i < nRemove; i++)
                {
                    Category c = null;
                    Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate { c = lp[i]; });
                    Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
                    {
                        el = role.RemoveSubCategoryFromCategory(c, currentCategory.ID);
                        if (el.Count > 0)
                        {
                            MessageForm form = new MessageForm();
                            form.ErrorList = el;
                            form.ShowDialog();
                            Close();
                        }
                        pbPBWaitingBar.Value++;
                    });
                }
                for (int i = 0; i < nAdd; i++)
                {
                    Category c = null;
                    Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate { c = parentCategories[i]; });
                    Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
                    {
                        el = role.InsertSubCategoryToCategory(c, currentCategory.ID);
                        if (el.Count > 0)
                        {
                            MessageForm form = new MessageForm();
                            form.ErrorList = el;
                            form.ShowDialog();
                            Close();
                        }
                        pbPBWaitingBar.Value++;
                    });
                }
                Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
                {
                    if (el.Count == 0)
                    {
                        el.Add(new ErrorItem("ErrorMessage", ErrorMessage.GetErrorMessage(1, userRole.ID_Language).Description));
                        MessageForm form = new MessageForm();
                        form.ErrorList = el;
                        form.ShowDialog();
                        lblPBTitle.Content = "Finish";
                    }
                });
            }
            else if (userRole.UserInfo.ID_Permission == 4)
            {
                PartnerRole role = (PartnerRole)userRole;
                List<ErrorItem> el = new List<ErrorItem>();
                int nAdd = 0;
                Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate { nAdd = parentCategories.Count; });
                int nRemove = 0;
                Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate { nRemove = parentCategories.Count; });

                Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
                {
                    gridProgressBar.Visibility = Visibility.Visible;
                    pbPBWaitingBar.Maximum = (double)(nAdd + nRemove - 1);
                    pbPBWaitingBar.Minimum = 0;
                    pbPBWaitingBar.Value = 0;
                });
                for (int i = 0; i < nAdd; i++)
                {
                    Category c = null;
                    Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate { c = parentCategories[i]; });
                    Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
                    {
                        el = role.InsertSubCategoryToCategory(currentCategory, c.ID);
                        if (el.Count > 0)
                        {
                            MessageForm form = new MessageForm();
                            form.ErrorList = el;
                            form.ShowDialog();
                            Close();
                        }
                        pbPBWaitingBar.Value++;
                    });
                }
                for (int i = 0; i < nRemove; i++)
                {
                    Category c = null;
                    Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate { c = parentCategories[i]; });
                    Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
                    {
                        el = role.InsertSubCategoryToCategory(currentCategory, c.ID);
                        if (el.Count > 0)
                        {
                            MessageForm form = new MessageForm();
                            form.ErrorList = el;
                            form.ShowDialog();
                            Close();
                        }
                        pbPBWaitingBar.Value++;
                    });
                }
                Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
                {
                    if (el.Count == 0)
                    {
                        el.Add(new ErrorItem("ErrorMessage", ErrorMessage.GetErrorMessage(1, userRole.ID_Language).Description));
                        MessageForm form = new MessageForm();
                        form.ErrorList = el;
                        form.ShowDialog();
                        lblPBTitle.Content = "Finish";
                    }
                });
            }
            Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
            {
                Thread.Sleep(100);
                Close();
            });
        }

        private void CreateCategory()
        {
            Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
            {
                lblPBTitle.Content = "Creating";
                pbPBWaitingBar.Minimum = 0.0;
                pbPBWaitingBar.Maximum = (2 + detailCategories.Count) * 1.0;
                pbPBWaitingBar.Value = 0.0;

            });
            List<ErrorItem> errorList = new List<ErrorItem>();
            OwnerRole role = (OwnerRole)userRole;
            Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
            {
                errorList = role.InsertCategory(ref currentCategory);
                if (errorList.Count > 0)
                {
                    MessageForm form = new MessageForm();
                    form.ErrorList = errorList;
                    form.ShowDialog();
                    Close();
                }
                pbPBWaitingBar.Value++;
            });

            foreach(DetailCategory dc in detailCategories)
            {
                Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
                {
                    if (dc.Name != "")
                    {
                        dc.ID_Category = currentCategory.ID;
                        errorList = role.EditDetailCategoryInformation(dc);
                        if (errorList.Count > 0)
                        {
                            MessageForm form = new MessageForm();
                            form.ErrorList = errorList;
                            form.ShowDialog();
                            Close();
                        }
                    }
                    pbPBWaitingBar.Value++;
                });
            }
            Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
            {
                if (errorList.Count == 0)
                {
                    errorList.Add(new ErrorItem("ErrorMessage", ErrorMessage.GetErrorMessage(1, userRole.ID_Language).Description));
                    MessageForm form = new MessageForm();
                    form.ErrorList = errorList;
                    form.ShowDialog();
                }
            });
            Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
            {
                lblPBTitle.Content = "Created";
                pbPBWaitingBar.Value++;
            });
            Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
            {
                Thread.Sleep(100);
                Close();
            });
        }

        private void EditCategory()
        {
            List<ErrorItem> errorList = new List<ErrorItem>();
            OwnerRole role = (OwnerRole)userRole;
            Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
            {
                lblPBTitle.Content = "Editing";
                pbPBWaitingBar.Minimum = 0.0;
                pbPBWaitingBar.Maximum = (2 + detailCategories.Count) * 1.0;
                pbPBWaitingBar.Value = 0.0;

                errorList = role.EditCategoryInformation(currentCategory);

            });
            Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
            {
                if (errorList.Count > 0)
                {
                    MessageForm form = new MessageForm();
                    form.ErrorList = errorList;
                    form.ShowDialog();
                    Close();
                }
                pbPBWaitingBar.Value++;
            });
            Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
                {
                    foreach (DetailCategory dc in detailCategories)
                    {
                        Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
                        {
                            errorList = role.EditDetailCategoryInformation(dc);
                            if (errorList.Count > 0)
                            {
                                MessageForm form = new MessageForm();
                                form.ErrorList = errorList;
                                form.ShowDialog();
                                Close();
                            }
                            pbPBWaitingBar.Value++;
                        });
                    }
                });

            Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
            {
                if (errorList.Count == 0)
                {
                    errorList.Add(new ErrorItem("ErrorMessage", ErrorMessage.GetErrorMessage(1, userRole.ID_Language).Description));
                    MessageForm form = new MessageForm();
                    form.ErrorList = errorList;
                    form.ShowDialog();
                }
            });
            Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
            {
                lblPBTitle.Content = "Edited";
                pbPBWaitingBar.Value++;
            });
            Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
            {
                Thread.Sleep(100);
                Close();
            });
        }

        private void LoadImagesList(int id_Permission, int id_Category)
        {
            double value = 0;
            if (id_Permission == 1)
            {
                OwnerRole role = (OwnerRole)userRole;
                if (id_Category > 0)
                {
                    List<VietImage.Models.Image> lUploadedImages = null;
                    Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
                    {
                        countPages = 0;
                        lUploadedImages = role.ViewNotPublishImages(id_Category, countPerPage, (currentPage - 1) * countPerPage, out countImages);
                        countPages = countImages / countPerPage;
                        if (countImages > countPerPage * countPages)
                            countPages++;
                        double max = 1;
                        pbPBWaitingBar.SetValue(ProgressBar.MaximumProperty, max);
                    });
                    Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate { pbPBWaitingBar.SetValue(ProgressBar.ValueProperty, value); });
                    Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate { omUploadedImages.Clear(); });
                    Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate { value++; pbPBWaitingBar.Value++; });

                    if (lUploadedImages != null)
                    {
                        for (int i = 0; i < countPerPage && i < countImages && i < lUploadedImages.Count; i++)
                        {
                            VietImage.Models.Image img = lUploadedImages[i].Clone();
                            Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate { omUploadedImages.Add(img); value++; pbPBWaitingBar.Value++; });
                            Thread.Sleep(50);
                        }
                    }
                }
            }
            else if (id_Permission == 4)
            {
                PartnerRole role = (PartnerRole)userRole;
                if (id_Category > 0)
                {
                    List<VietImage.Models.Image> lUploadedImages = null;
                    Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
                    {
                        countPages = 0;
                        double max = 1;
                        lUploadedImages = role.ViewNotPublishImages(id_Category, countPerPage, (currentPage - 1) * countPerPage, out countImages);
                        countPages = countImages / countPerPage;
                        if (countImages > countPerPage * countPages)
                            countPages++;
                        pbPBWaitingBar.SetValue(ProgressBar.MaximumProperty, max);
                    });
                    Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate { pbPBWaitingBar.SetValue(ProgressBar.ValueProperty, value); });
                    Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate { omUploadedImages.Clear(); });
                    Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate { value++; pbPBWaitingBar.Value++; });

                    if (lUploadedImages != null)
                    {
                        for (int i = 0; i < countPerPage && i < countImages && i < lUploadedImages.Count; i++)
                        {
                            VietImage.Models.Image img = lUploadedImages[i].Clone();
                            Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate { omUploadedImages.Add(img); value++; pbPBWaitingBar.Value++; });
                            Thread.Sleep(50);
                        }
                    }
                }
            }
            Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
            {
                Thread.Sleep(100);
                Close();
            });
        }

        private void LoadImagesList(int id_Permission)
        {
            double value = 0;
            Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate { pbPBWaitingBar.Value = 0.0; });
            if (id_Permission == 1)
            {
                OwnerRole role = (OwnerRole)userRole;
                countPages = 0;
                List<VietImage.Models.Image> lHistoryImages = null;
                Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
                {
                    if (prior == 0)
                        lHistoryImages = role.ViewUploadedImagesDaily(countPerPage, (currentPage - 1) * countPerPage, out countImages, currentDate);
                    else if (prior == 2)
                        lHistoryImages = role.ViewUploadedImagesMonthly(countPerPage, (currentPage - 1) * countPerPage, out countImages, currentMonth, currentYear);
                    else if (prior == 1)
                        lHistoryImages = role.ViewUploadedImagesWeekly(countPerPage, (currentPage - 1) * countPerPage, out countImages, currentWeek.BeginDate, currentWeek.EndDate);
                    countPages = countImages / countPerPage;
                    if (countImages > countPerPage * countPages)
                        countPages++;
                    double max = 1;
                    pbPBWaitingBar.SetValue(ProgressBar.MaximumProperty, max);
                });
                Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate { pbPBWaitingBar.SetValue(ProgressBar.ValueProperty, value); });
                Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate { omHistoryImages.Clear(); });
                Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate { value++; pbPBWaitingBar.Value++; });

                if (lHistoryImages != null)
                {
                    for (int i = 0; i < countPerPage && i < countImages && i < lHistoryImages.Count; i++)
                    {
                        VietImage.Models.Image img = lHistoryImages[i].Clone();
                        Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate { omHistoryImages.Add(img); value++; pbPBWaitingBar.Value++; });
                        Thread.Sleep(50);
                    }
                }

            }
            else if (id_Permission == 4)
            {
                PartnerRole role = (PartnerRole)userRole;
                countPages = 0;
                List<VietImage.Models.Image> lHistoryImages = null;
                Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
                {
                    if (prior == 0)
                        lHistoryImages = role.ViewUploadedImagesDaily(countPerPage, (currentPage - 1) * countPerPage, out countImages, currentDate);
                    else if (prior == 2)
                        lHistoryImages = role.ViewUploadedImagesMonthly(countPerPage, (currentPage - 1) * countPerPage, out countImages, currentMonth, currentYear);
                    else if (prior == 1)
                        lHistoryImages = role.ViewUploadedImagesWeekly(countPerPage, (currentPage - 1) * countPerPage, out countImages, currentWeek.BeginDate, currentWeek.EndDate);
                    countPages = countImages / countPerPage;
                    if (countImages > countPerPage * countPages)
                        countPages++;
                    double max = 1;
                    pbPBWaitingBar.SetValue(ProgressBar.MaximumProperty, max);
                });
                Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate { pbPBWaitingBar.SetValue(ProgressBar.ValueProperty, value); });
                Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate { omHistoryImages.Clear(); });
                Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate { value++; pbPBWaitingBar.Value++; });

                if (lHistoryImages != null)
                {
                    for (int i = 0; i < countPerPage && i < countImages && i < lHistoryImages.Count; i++)
                    {
                        VietImage.Models.Image img = lHistoryImages[i].Clone();
                        Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate { omHistoryImages.Add(img); value++; pbPBWaitingBar.Value++; });
                        Thread.Sleep(50);
                    }
                }
            }
            Dispatcher.Invoke(DispatcherPriority.Background, (System.Action)delegate
            {
                Thread.Sleep(100);
                Close();
            });
        }

        private void gridProgressBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

            try
            {
                this.DragMove();
            }
            catch (Exception ex)
            {
            }
        }
	}
}